package core.xmpp;
import core.IncomeMessage;
import core.Main;
import core.IMessageSender;
import java.util.UUID;
import java.util.logging.Level;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;
import tools.Logger;

/**
 * Init Income Message
 * @author dcrm
 */
public class IncomeMsg implements IMessageSender, Runnable {

    private XMPPConnection XMPP_CONNECTION;
    private String IncomingMessage;
    private Chat CHAT;
    private Message MSG;
    private int MSG_MAX_PART; // Max Size part MSG;
    private CoreXMPP CORE;
    
    private Thread thread;
    private final String THREAD_NAME ="XMPPMSG["+UUID.randomUUID()+"]";

    IncomeMsg(Chat chat, Message message, XMPPConnection connection, CoreXMPP core) {
        this.CORE=core;
        this.CHAT=chat;
        this.MSG=message;
        this.XMPP_CONNECTION=connection;
        this.IncomingMessage=message.getBody();
        this.MSG_MAX_PART=2000;
        thread = new Thread(this);
        thread.setName(THREAD_NAME);
        thread.start();
    }

    public void run() {
        try {
            new IncomeMessage(IncomingMessage, this.MSG.getFrom().toString(), CORE.i_Jid_Master.toString(), MSG_MAX_PART, THREAD_NAME, this);
        } catch (Exception ex) {Logger.writelnErr(ex.getMessage());}
    }

    public void sendBAdmReport(boolean b,String s)
    {
        try { CORE.sendIMADM(s + " - state: " + ((b) ? "ON" : "OFF"));
        } catch (Exception ex) {Logger.writelnErr(ex.getMessage());}
    }

    public void sendAdMsg(String s)
    {
        try { CORE.sendIMADM(s);
        } catch (Exception ex) {Logger.writelnErr(ex.getMessage());}
    }

    public void sendAdEMsg(String msg) {
        sendAdMsg(msg);
    }

    public void sendMessage(String to, String msg) {
        CORE.sendIM(msg, CHAT);
    }

    public void sendExtendedMessage(String to, String msg) {
        sendMessage(to, msg);
    }

    public void reConnect() {
        CORE.disconnect(false);
    }

    public void disconnect(boolean exit) {
        CORE.disconnect(exit);
    }

    public String getInfo() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
